package de.unimannheim.ki.usermanagement;



import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import de.umass.lastfm.Caller;
import de.umass.lastfm.PaginatedResult;
import de.umass.lastfm.Track;
import de.umass.lastfm.User;

public class LastFMUserHandler {

	private static String APIKEY = "7641e28f7cdec04671e0c3c19455dc8f";
	//private LastFMAPIHandler lastfmhandler;

	public LastFMUserHandler(){ 
		  Caller.getInstance().setUserAgent("tst");
		//lastfmhandler = new LastFMAPIHandler();
	}
	
	
	public List<Track> getUserTracks(String userid){
		HashMap<String, Track> hmap = new HashMap<String, Track>();
		List<Track> usertracks = new ArrayList<Track>();
		PaginatedResult<Track> resultset;
		Collection<Track> intermediate;
	
		int page = 1;

		resultset = User.getRecentTracks(userid, page, 200,  APIKEY);
		intermediate = resultset.getPageResults();
		
		for (Track track : intermediate) {
			if( ! hmap.containsKey(track.getUrl())){
				if(track.getUrl()!= null){
					hmap.put(track.getUrl(),track);
					usertracks.add(track);
				}
			}
		}
		


		resultset = User.getLovedTracks(userid, APIKEY);
	
		intermediate = resultset.getPageResults();
		for (Track track : intermediate) {
			
			if( ! hmap.containsKey(track.getUrl())){
				if(track.getUrl()!= null){
					hmap.put(track.getUrl(),track);
					usertracks.add(track);
				}
			}
		}
		return usertracks;
	}
	
}
